<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-input v-model="dataForm.keyword.phone" placeholder="输入手机号查找" clearable style="width:150px"></el-input>
      </el-form-item>
      <el-form-item label="是否VIP:" style="display: inline-block;">
        <el-select v-model="dataForm.keyword.vip" style="width: 120px;" @change="getDataList(1)">
          <el-option
            v-for="item in vipOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="来源:" style="display: inline-block;">
        <el-select v-model="dataForm.keyword.source" style="width: 120px;" @change="getDataList(1)">
          <el-option
            v-for="item in sourceList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="display: inline-block;">
        <el-date-picker
          v-model="dataForm.keyword.stime"
          type="datetime"
          value-format="timestamp"
          :picker-options="pickerOptionsStart"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item style="display: inline-block;">
        <el-date-picker
          v-model="dataForm.keyword.etime"
          type="datetime"
          value-format="timestamp"
          :picker-options="pickerOptionsEnd"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
      @row-click="updateHandle">
      <el-table-column
        prop="avatar"
        header-align="center"
        align="center"
        label="用户头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" style="width: 60px;height: 60px;border-radius: 50%;">
        </template>
      </el-table-column>
      <el-table-column
        prop="phone"
        header-align="center"
        align="center"
        label="手机号">
      </el-table-column>
      <el-table-column
        prop="wxNickName"
        header-align="center"
        align="center"
        label="微信名">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        prop="genderCn"
        label="性别">
        <!--<template slot-scope="scope">-->
        <!--{{scope.row.gender==1?'男':(scope.row.gender==2?'女':'未知')}}-->
        <!--</template>-->
      </el-table-column>
      <el-table-column
        prop="nickNameChild"
        header-align="center"
        align="center"
        label="宝宝昵称">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        prop="genderCnChild"
        label="宝宝性别">
        <!--<template slot-scope="scope">-->
        <!--{{scope.row.genderChild==1?'男':(scope.row.genderChild==2?'女':'未知')}}-->
        <!--</template>-->
      </el-table-column>
      <el-table-column
        prop="vipCN"
        header-align="center"
        align="center"
        label="制药VIP">
        <!--<template slot-scope="scope">-->
        <!--{{scope.row.vip==1?'是':'否'}}-->
        <!--</template>-->
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="绘本数量">
      </el-table-column>
      <el-table-column
        prop="sourceName"
        header-align="center"
        align="center"
        label="来源">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center" width="180"
        label="注册时间">
        <template slot-scope="scope" v-if="scope.row.joinTime">
          <!-- {{utils.formatUnixtimestamp(scope.row.joinTime)}} -->
          {{scope.row.createTime}}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './pbuser-add-or-update'

  export default {
    data () {
      return {
        dataForm: {
          keyword: {
            phone: '',
            vip: '',
            source: '',
            stime: '',
            etime: ''
          }
        },
        pickerOptionsStart: {
          disabledDate: time => {
            let endDateVal = this.dataForm.keyword.etime;
            if (endDateVal) {
              return time.getTime() > new Date(endDateVal).getTime();
            }
          }
        },
        pickerOptionsEnd: {
          disabledDate: time => {
            let beginDateVal = this.dataForm.keyword.stime;
            if (beginDateVal) {
              return (
                time.getTime() <
                new Date(beginDateVal).getTime() - 1 * 24 * 60 * 60 * 1000
              );
            }
          }
        },
        vipOptions: [{id: '', name: '全部'}, {id: '0', name: '否'}, {id: '1', name: '是'}],
        dataList: [],
        sourceList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
      this.getSource()
    },
    methods: {
      // 获取数据列表
      getDataList (formSearch) {
        // 搜索框来源
        if (formSearch === 1) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/picbook/pbuser/list'),
          method: 'post',
          data: this.$http.adornData({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keyword': this.dataForm.keyword
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 获取来源
      getSource () {
        this.$http({
          url: this.$http.adornUrl('/picbook/combobox/listUserSource'),
          method: 'post'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.sourceList = data.data
          } else {
            this.sourceList = []
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 详情修改
      updateHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row.id)
        })
      }
    }
  }
</script>
